define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    merchantInfo: {},
                    MerchantName:'',
                    MerchantId:'',
                    showType:1,
                    storeData:{},
                    logoString:[]
                };
            },
            el: '#pageMerchantInfo',
            created: function() {
                this.fetchData();
                 // app.area(".DestinationArea");
                  var _this = this;
                 app.cityPicker("#DestinationArea",{
                        title: "选择地区",
                        onChange: function (picker, values, displayValues) {
                          console.log( values);
                          //   console.log( displayValues);
                            _this.destinationAreaValues = values;
                            _this.destinationArea = displayValues;
                            $("#District").val(values[2]);
                            $("#City").val(values[1]);
                            $("#Province").val(values[0]);

                            // $("#county_name").val(displayValues[2]);
                            // $("#city_name").val(displayValues[1]);
                            // $("#province_name").val(displayValues[0]);

                            
                            // that.cityName = displayValues[1];
                            // that.countyName = displayValues[2];
                        }
                });
               
            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    
                    this.getInfo(); 
                    this.getStoreData();
                },
                getInfo:function(){
                    var that = this;
                    if( that.loading == 1){
                        return ;
                     }
                     that.loading = 1;
                    
                    var user_info =  app.getUserInfo();
                    
                    app.get("apiv2/mch_my_bank/merchantInfo", {
                       
                    }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.merchantInfo = res.data.MerchantDetail;
                      // that.MerchantDetail = res.data.MerchantDetail;
                      that.MerchantName=  res.data.MerchantName;
                      that.MerchantId =  res.data.MerchantId;

                      
                      
                    });
                },


              getStoreData:function(){
                  var that = this ;
                     that.loading = 1;
                     
                    app.get("api/user/info", {
                      fields :  'store'
                    }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.storeData = res.data.store; 
                      if(that.storeData.logo){
                          that.logoString = JSON.stringify([{name:'logo',file_url:that.storeData.logo}]);
                       }
                       that.$nextTick(function(){
                         app.upload($("#storeLogo"));
                       });
                    });
                },
                
                saveStore:function(){
                   var that = this;
                   if(that.loading==1){
                      return ;
                   }
                   this.loading=1;
                   var post ={
                      logo:$('[name="logo"]').val(),
                      store_id:$('[name="store_id"]').val(),
                      store_name:$('[name="Alias"]').val(),
                      phone:$('[name="ContactMobile"]').val(),
                      address:$('[name="DestinationArea"]').val()+$('[name="Address"]').val(),
                   };
                  // $("#formNewBook").find("input").each(function(i,input){
                  //    post[$(input).attr('name')] = $(input).val();
                  // });
                  
                  app.post("apiv2/store/save",post,function(res){
                        that.loading=0;
                            if(res.code!=200){
                               app.alert(res.msg);
                               return ;
                            }
                           
              
                    });
                },
            


                updateMerchantInfo:function(){
                   this.saveStore();
                   var that = this;
                   var data = $("#InfoForm").serializeArray();
                    data.push({
                        name:"method",
                        value:'updateMerchantInfo'
                    });
                   app.post("/apiv2/mch_my_bank",data,function(res){
                       if(res.code!=200){
                        app.alert(res.msg);
                        return;
                       }
                       app.alert("修改成功");
                       that.getInfo();
                       that.showType=1;
                   });
                }
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
